package matrici;

import java.io.*;
import java.text.DecimalFormat;
import java.util.*;

public class Interpolazione {

    final int ESP = 8;
    final String PATH = "C:\\Program Files\\gp443win32\\gnuplot\\binary";
    Process process;

    Interpolazione() {
        OperazioniSuMatrici o = new OperazioniSuMatrici();
        BufferedReader in = null;
        ArrayList<Double> tx = new ArrayList();
        ArrayList<Double> ty = new ArrayList();
        double[][] A = null;
        double[][] y = null;

        try {
            in = new BufferedReader(new InputStreamReader(new FileInputStream("dati.dat")));
            String line = in.readLine();
            while (line != null) {
                String[] temp = line.split("\t");
                if (temp != null) {
                    tx.add(new Double(temp[0]));
                    ty.add(new Double(temp[1]));
                }
                line = in.readLine();
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                in.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

        y = new double[ty.size()][1];

        for (int i = 0; i < ty.size(); i++) {
            y[i][0] = ty.get(i);
        }

        A = new double[tx.size()][ESP + 1];
        for (int i = 0; i < tx.size(); i++) {
            for (int j = 0; j < ESP + 1; j++) {
                A[i][j] = Math.pow(tx.get(i), ESP - j);
            }
        }

        double[][] pseudoInversa = o.inverse(A);
        double[][] coefficenti = o.product(pseudoInversa, y);

        System.out.println(o.stampaMatrice(coefficenti));

        int xMax = Integer.MIN_VALUE;
        int yMax = Integer.MIN_VALUE;
        int xMin = Integer.MAX_VALUE;
        int yMin = Integer.MAX_VALUE;

        for (int i = 0; i < tx.size(); i++) {
            if (tx.get(i) >= xMax) {
                xMax = tx.get(i).intValue();
            }
            if (ty.get(i) >= yMax) {
                yMax = ty.get(i).intValue();
            }
            if (tx.get(i) <= xMin) {
                xMin = tx.get(i).intValue();
            }
            if (ty.get(i) <= yMin) {
                yMin = ty.get(i).intValue();
            }
        }
        int dx = xMax-xMin;
        int dy = yMax-yMin;
        xMax+=dx*0.5;
        yMax+=dy*0.5;
        xMin-=dx*0.5;
        yMin-=dy*0.5;

        DecimalFormat df = new DecimalFormat("##0.####");
        String function = "";

        for (int i = 0; i <= ESP; i++) {
            function += df.format(coefficenti[i][0]);
            for (int j = i; j < ESP; j++) {
                function += "*x";
            }
            if (i < ESP) {
                if (!df.format(coefficenti[i + 1][0]).contains("-")) {
                    function += "+";
                }
            }
        }

        function = function.replace(",", ".");

        String axisRanges = "set xrange [" + xMin + ":" + xMax + "]; set yrange [" + yMin + ":" + yMax + "]";
        String axisLabels = "set xlabel \\\"" + "X" + "\\\"; set ylabel \\\"" + "Y" + "\\\" ";
        String plotCmd = "plot \\\"dati.dat\\\" with points, " + function;

        String cmd = "";
        cmd = "\"" + PATH + "\\gnuplot.exe\" -p -e \"" + axisRanges + ";" + axisLabels + ";" + plotCmd + "\"";

        try {
            ProcessBuilder pB = new ProcessBuilder(cmd);
            process = pB.start();
        } catch (IOException ex) {
            System.err.println("Errore: " + ex.getMessage());
        }
        try {
            Thread.sleep(10000);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String[] args) {
        new Interpolazione();
    }
}
